package com.itheima.consumer.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class MqConfig {

    //创建交换机
    @Bean
    public DirectExchange simpleExchange(){
        return new DirectExchange("simple.direct");
    }

    //创建队列
    @Bean
    public Queue simoleQueue(){
        return QueueBuilder.durable("simple.queue")
                .ttl(10000)
                .deadLetterExchange("dlx.direct")
                .deadLetterRoutingKey("dlx")
                .build();
    }

    //绑定
    public Binding simpleBinding(DirectExchange simpleExchange,Queue simoleQueue){
        return BindingBuilder.bind(simoleQueue).to(simpleExchange).with("simple");
    }


//创建交换机
    @Bean
    public DirectExchange dlxExchange(){
        return new DirectExchange("dlx.direct");
    }
    //创建队列
    @Bean
    public Queue dlxQueue(){
        return new Queue("dlx.queue");
    }
    //绑定
    @Bean
    public Binding dlxBinding(DirectExchange dlxExchange,Queue dlxQueue){
        return BindingBuilder.bind(dlxQueue).to(dlxExchange).with("dlx");
    }

//定义republishMessageRecoverer
    @Bean
    public MessageRecoverer republishMessageRecoverer(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate,"error.direct","error");
    }

    //创建交换机
    @Bean
    public DirectExchange errorMessageExchange(){
        return new DirectExchange("hmall.error.exchange");
    }
    //创建队列
    @Bean
    public Queue errorQueue(){
        return new Queue("error.queue");
    }
    //绑定
    @Bean
    public Binding errorBinding(DirectExchange errorMessageExchange,Queue errorQueue){
        return BindingBuilder.bind(errorQueue).to(errorMessageExchange).with("error");
    }

    //创建交换机
    @Bean
    public FanoutExchange hmallFanoutExchange(){
        return new FanoutExchange("hmall.fanout.exchange1");
    }


    /**
     * 创建队列
     */
    @Bean
    public Queue hmallFanoutQueue1(){
        return new Queue("hmall.fanout.queue1");
    }

    /**
     * 创建绑定关系
     */
   @Bean
    public Binding hmallFanoutQueueBingding(FanoutExchange hmallFanoutExchange,Queue hmallFanoutQueue1){
       return BindingBuilder.bind(hmallFanoutQueue1).to(hmallFanoutExchange);
   }

}
